'
' Python-C API
'
Imports System.Runtime.InteropServices


Public Module CAPI
	'
	' constanst
	'
	Const PYTHONDLL As String = "python25.dll"	' change to fit your version

	Enum CompilationMode As Integer
		Exec = 257
		Eval = 258
	End Enum

	Enum CompareOperator
		Py_LT = 0
		Py_LE = 1
		Py_EQ = 2
		Py_NE = 3
		Py_GT = 4
		Py_GE = 5
	End Enum

	'
	' state
	'
	<DllImport(PYTHONDLL)> Public Sub Py_Initialize()
	End Sub
	<DllImport(PYTHONDLL)> Public Sub Py_Finalize()
	End Sub
	<DllImport(PYTHONDLL)> Public Sub Py_IsInitialized()
	End Sub
	<DllImport(PYTHONDLL)> Public Function PyThreadState_Get() As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Sub PyEval_InitThreads()
	End Sub
	<DllImport(PYTHONDLL)> Public Sub PyEval_AcquireLock()
	End Sub
	<DllImport(PYTHONDLL)> Public Sub PyEval_ReleaseLock()
	End Sub
	<DllImport(PYTHONDLL)> Public Sub PyEval_AcquireThread(ByVal tstate As IntPtr)
	End Sub
	<DllImport(PYTHONDLL)> Public Sub PyEval_ReleaseThread(ByVal tstate As IntPtr)
	End Sub
	<DllImport(PYTHONDLL)> Public Function PyEval_SaveThread() As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Sub PyEval_RestoreThread(ByVal tstate As IntPtr)
	End Sub
	<DllImport(PYTHONDLL)> Public Function PyGILState_Ensure() As Int32
	End Function
	<DllImport(PYTHONDLL)> Public Sub PyGILState_Release(ByVal gilstate As Int32)
	End Sub

	'
	' evaluation
	'
	<DllImport(PYTHONDLL)> Public Function PyRun_String( _
	ByVal code As String, ByVal mode As CompilationMode, ByVal globals As IntPtr, ByVal locals As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function Py_CompileString( _
	ByVal code As String, ByVal filename As String, ByVal mode As CompilationMode) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyRun_SimpleString(ByVal code As String) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyEval_GetBuiltins() As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyEval_GetGlobals() As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyEval_GetLocals() As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyEval_GetFrame() As IntPtr
	End Function

	'
	' exceptions
	'
	<DllImport(PYTHONDLL)> Public Function PyErr_Occurred() As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Sub PyErr_Clear()
	End Sub
	<DllImport(PYTHONDLL)> Public Sub PyErr_Fetch( _
	ByRef ptype As IntPtr, ByRef pvalue As IntPtr, ByRef ptraceback As IntPtr)
	End Sub
	<DllImport(PYTHONDLL)> Public Sub PyErr_NormalizeException( _
	ByRef ptype As IntPtr, ByRef pvalue As IntPtr, ByRef ptraceback As IntPtr)
	End Sub
	<DllImport(PYTHONDLL)> Public Sub PyErr_Print()
	End Sub
	<DllImport(PYTHONDLL)> Public Function PyErr_GivenExceptionMatches( _
	ByVal given As IntPtr, ByVal exc As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Sub PyErr_SetString(ByVal type As IntPtr, ByVal message As String)
	End Sub
	<DllImport(PYTHONDLL)> Public Sub PyErr_SetObject(ByVal type As IntPtr, ByVal value As IntPtr)
	End Sub

	'
	' reference counting
	'
	<DllImport(PYTHONDLL)> Public Sub Py_IncRef(ByVal inst As IntPtr)
	End Sub
	<DllImport(PYTHONDLL)> Public Sub Py_DecRef(ByVal inst As IntPtr)
	End Sub

	'
	' modules
	'
	<DllImport(PYTHONDLL)> Public Function PyImport_ImportModuleEx( _
	ByVal modname As String, ByVal globals As IntPtr, ByVal locals As IntPtr, ByVal fromlist As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyImport_Import(ByVal modname As String) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyImport_ImportFrozenModule(ByVal modname As String) As Integer
	End Function

	'
	' object protocol
	'
	<DllImport(PYTHONDLL)> Public Function PyObject_HasAttrString( _
	ByVal inst As IntPtr, ByVal name As String) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_HasAttr( _
	ByVal inst As IntPtr, ByVal name As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_GetAttrString( _
	ByVal inst As IntPtr, ByVal name As String) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_GetAttr( _
	ByVal inst As IntPtr, ByVal name As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_SetAttrString( _
	ByVal inst As IntPtr, ByVal name As String, ByVal value As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_SetAttr( _
	ByVal inst As IntPtr, ByVal name As IntPtr, ByVal value As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_DelAttrString( _
	ByVal inst As IntPtr, ByVal name As String) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_DelAttr( _
	ByVal inst As IntPtr, ByVal name As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Repr(ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Str(ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Unicode(ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Hash(ByVal inst As IntPtr) As Int32
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_IsTrue(ByVal inst As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Not(ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Type(ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Size(ByVal inst As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Dir(ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_GetIter(ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Call( _
	ByVal inst As IntPtr, ByVal args As IntPtr, ByVal kwargs As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_CallObject( _
	ByVal inst As IntPtr, ByVal args As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_GetItem( _
	ByVal inst As IntPtr, ByVal key As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_SetItem( _
	ByVal inst As IntPtr, ByVal key As IntPtr, ByVal value As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_DelItem( _
	ByVal inst As IntPtr, ByVal key As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Cmp( _
	ByVal obj1 As IntPtr, ByVal obj2 As IntPtr, ByRef res As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_Compare( _
	ByVal obj1 As IntPtr, ByVal obj2 As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_RichCompare( _
	ByVal o1 As IntPtr, ByVal o2 As IntPtr, ByVal opid As CompareOperator) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_RichCompareBool( _
	ByVal o1 As IntPtr, ByVal o2 As IntPtr, ByVal opid As CompareOperator) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_IsSubclass( _
	 ByVal derived As IntPtr, ByVal cls As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyObject_IsInstance( _
	ByVal inst As IntPtr, ByVal cls As IntPtr) As Integer
	End Function

	'
	' type objects
	'
	<DllImport(PYTHONDLL)> Public Function PyType_IsSubtype(ByVal derived As IntPtr, ByVal type As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyType_HasFeature(ByVal type As IntPtr, ByVal feature As Integer) As Integer
	End Function
	Function PyObject_TypeCheck(ByVal obj As IntPtr, ByVal type As IntPtr) As Boolean
		If obj.Equals(type) Then Return True
		Return CBool(PyType_IsSubtype(PyObject_Type(obj), type))
	End Function

	'
	' bool objects
	'
	<DllImport(PYTHONDLL)> Public Function PyBool_FromLong(ByVal value As Integer) As IntPtr
	End Function

	'
	' int objects
	'
	<DllImport(PYTHONDLL)> Public Function PyInt_FromLong(ByVal value As Int32) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyInt_FromString( _
	ByVal value As String, ByVal pend As IntPtr, ByVal base As Integer) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyInt_AsLong(ByVal inst As IntPtr) As Int32
	End Function

	'
	' long objects
	'
	<DllImport(PYTHONDLL)> Public Function PyLong_FromLong(ByVal value As Int32) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyLong_FromLongLong(ByVal value As Int64) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyLong_FromUnsignedLong(ByVal value As UInt32) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyLong_FromUnsignedLongLong(ByVal value As UInt64) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyLong_FromDouble(ByVal value As Double) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyLong_FromString( _
	ByVal value As String, ByVal pend As IntPtr, ByVal base As Integer) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyLong_AsLong(ByVal inst As IntPtr) As Int32
	End Function
	<DllImport(PYTHONDLL)> Public Function PyLong_AsUnsignedLong(ByVal inst As IntPtr) As UInt32
	End Function
	<DllImport(PYTHONDLL)> Public Function PyLong_AsLongLong(ByVal inst As IntPtr) As Int64
	End Function
	<DllImport(PYTHONDLL)> Public Function PyLong_AsUnsignedLongLong(ByVal inst As IntPtr) As UInt64
	End Function
	<DllImport(PYTHONDLL)> Public Function PyLong_AsDouble(ByVal inst As IntPtr) As Double
	End Function

	'
	' float objects
	'
	<DllImport(PYTHONDLL)> Public Function PyFloat_FromString(ByVal value As String, ByVal pend As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyFloat_FromDouble(ByVal value As Double) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyFloat_AsDouble(ByVal inst As IntPtr) As Double
	End Function

	'
	' str objects
	'
	<DllImport(PYTHONDLL)> Public Function PyString_FromString(ByVal value As String) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyString_FromStringAndSize( _
	ByVal value As String, ByVal size As Integer) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyString_AsString(ByVal inst As IntPtr) As String
	End Function
	<DllImport(PYTHONDLL)> Public Function PyString_Size(ByVal inst As IntPtr) As Integer
	End Function

	'
	' unicode objects
	'
	<DllImport(PYTHONDLL, CharSet:=CharSet.Unicode)> Public Function PyUnicode_FromUnicod( _
	ByVal value As String, ByVal size As Integer) As IntPtr
	End Function
	<DllImport(PYTHONDLL, CharSet:=CharSet.Unicode)> Public Function PyUnicode_FromObject( _
	ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL, CharSet:=CharSet.Unicode)> Public Function PyUnicode_AsUnicod( _
	ByVal inst As IntPtr) As String
	End Function
	<DllImport(PYTHONDLL)> Public Function PyUnicode_Size(ByVal inst As IntPtr) As Integer
	End Function

	'
	' list objects
	'
	<DllImport(PYTHONDLL)> Public Function PyList_New(ByVal count As Integer) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyList_SetItem( _
	ByVal inst As IntPtr, ByVal index As Integer, ByVal value As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyList_GetItem( _
	ByVal inst As IntPtr, ByVal index As Integer) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyList_Size(ByVal inst As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyList_Append( _
	ByVal inst As IntPtr, ByVal value As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyList_AsTuple(ByVal inst As IntPtr) As Integer
	End Function

	'
	' tuple objects
	'
	<DllImport(PYTHONDLL)> Public Function PyTuple_New(ByVal count As Integer) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyTuple_GetItem( _
	ByVal inst As IntPtr, ByVal index As Integer) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyTuple_SetItem( _
	ByVal inst As IntPtr, ByVal index As Integer, ByVal value As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyTuple_Size(ByVal inst As IntPtr) As Integer
	End Function

	'
	' dict objects
	'
	<DllImport(PYTHONDLL)> Public Function PyDict_New() As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Sub PyDict_Clear(ByVal inst As IntPtr)
	End Sub
	<DllImport(PYTHONDLL)> Public Function PyDict_Contains( _
	ByVal inst As IntPtr, ByVal key As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_Copy(ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_GetItem( _
	ByVal inst As IntPtr, ByVal key As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_GetItemString( _
	ByVal inst As IntPtr, ByVal key As String) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_SetItem( _
	ByVal inst As IntPtr, ByVal key As IntPtr, ByVal value As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_SetItemString( _
	ByVal inst As IntPtr, ByVal key As String, ByVal value As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_DelItem( _
	ByVal inst As IntPtr, ByVal key As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_DelItemString( _
	ByVal inst As IntPtr, ByVal key As String) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_Size(ByVal inst As IntPtr) As Integer
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_Items(ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_Values(ByVal inst As IntPtr) As IntPtr
	End Function
	<DllImport(PYTHONDLL)> Public Function PyDict_Keys(ByVal inst As IntPtr) As IntPtr
	End Function


End Module
